package com.translate.web.config;


import javax.annotation.Resource;

import org.hibernate.validator.HibernateValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.translate.web.config.interceptor.AuthorizationInterceptor;
import com.translate.web.config.interceptor.CrossDomainRequestInterceptor;

/**
 * SpringMVC配置
 */
@Configuration
public class SpringMvcConfiguration implements WebMvcConfigurer {

	private MessageSource messageSource;

	@Autowired
	private AuthorizationInterceptor authorizationInterceptor;

	@Resource(name="crossDomainRequestInterceptor")
	private CrossDomainRequestInterceptor crossDomainRequestInterceptor;

	@Autowired
	public SpringMvcConfiguration(MessageSource messageSource) {
		this.messageSource = messageSource;
	}

	@Bean
	@Scope("singleton")
	public ObjectMapper objectMapper() {
		return new ResponseJsonMapper();
	}

	@Bean
	public Validator validator() {
		LocalValidatorFactoryBean validatorFactoryBean = new LocalValidatorFactoryBean();
		validatorFactoryBean.setProviderClass(HibernateValidator.class);
		validatorFactoryBean.setValidationMessageSource(messageSource);
		return validatorFactoryBean;
	}

	@Override
	public Validator getValidator() {
		return validator();
	}

	/**
	 * 添加拦截器
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// 拦截路径可自行配置多个 可用 ，分隔开

		String url[] = new String[] { 
			"/api/ShoppingCart/*", 
			"/api/userInfo/*", 
			"/api/order/*", 
			"/api/Pay/*",
			"/api/Collectedworks/*", 
			"/api/follow/*", 
			"/upload/*",
			"/payManager/*",
			"/changeRecord/*",
			"/recharge/*",
			"/order/*",
			"/invocieRecharge/*",
			"/preStoredRewardsConf/*",
			"/planceAnOrder/*",
			"/orderLog/*",
			"/noticeReaded/*",
			"/team/*",
			"/commissionManagement/*",
			"/commissionManagementDetail/*",
			"/api/ma/apply",
			"/center/*",
			"/withdrawal/*",
			"/notice/*",
			"/noticeCenter/*",
			"/translate/*"
		};

		registry.addInterceptor(crossDomainRequestInterceptor);

		registry.addInterceptor(authorizationInterceptor).addPathPatterns(url);
	}
}
